Game đuổi chó sa mạc lập trình bằng ngôn ngữ c#
19.245 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3 using Holoville.HOTween;
4 using Holoville.HOTween.Plugins;
5 using UnityEngine.UI;
6
7 public class GameManager : MonoBehaviour {
8 public Player player;
9 public GameObject pillarPrefab;
10 public float spawnTime = 4f;
11
12 [HideInInspector]
13 public bool isGameStart = false;
14 [HideInInspector]
15 public bool isGameOver = false;
16 Transform startButton, overButton, readyLabel, overLabel, tabLabel, titleLabel, rankBoard;
17 Vector3 posStartButton, posOverButton, posReadyLabel, posOverLabel, posTabLabel, posTitleLabel, posRankBoard;
18
19 int oldScore = 0;
20 int score = 0;
21
22 Text scoreLabel, resultScore, resultBestScore;
23 Text oldScoreLabel;
24
25 Image damageSprite;
26
27 Camera cam;
28 Vector3 camPosition, playerPosition;
29
30 //public SpawnPool pool;
31
32 void Start () {
33 Application.targetFrameRate = 60;
34 LoadOldScore();
35 cam = Camera.main.GetComponent<Camera>();
36 camPosition = cam.transform.position;
37 resultScore = GameObject.Find("ResultScore").GetComponentInChildren<Text>();
38 resultBestScore = GameObject.Find("ResultBestScore").GetComponentInChildren<Text>();
39 scoreLabel = GameObject.Find("GameScore").GetComponentInChildren<Text>();
40 oldScoreLabel = GameObject.Find("BestScore").GetComponentInChildren<Text>();
41
42 damageSprite = GameObject.Find("DamageSprite").GetComponent<Image>();
43
44 rankBoard = GameObject.Find("RankBoard").transform;
45 titleLabel = GameObject.Find("GameTitle").transform;
46 readyLabel = GameObject.Find("AreYouReady").transform;
47 overLabel = GameObject.Find("GameOver").transform;
48 tabLabel = GameObject.Find("TapToJump").transform;
49 overButton = GameObject.Find("OverButton").transform;
50 startButton = GameObject.Find("StartButton").transform;
51
52 posRankBoard = rankBoard.localPosition;
53 posTitleLabel = titleLabel.localPosition;
54 posStartButton = startButton.localPosition;
55 posOverButton = overButton.localPosition;
56 posReadyLabel = readyLabel.localPosition;
57 posOverLabel = overLabel.localPosition;
58 posTabLabel = tabLabel.localPosition;
59
60 rankBoard.localPosition = Vector3.right * 700f;
61 titleLabel.localPosition = Vector3.up * 700f;
62 readyLabel.localPosition = Vector3.left * 700f;
63 startButton.localPosition = Vector3.down * 700f;
64 overButton.localPosition = Vector3.down * 700f;
65 overLabel.localPosition = Vector3.up * 700f;
66 tabLabel.localPosition = Vector3.right * 700f;
67
68
69
70 if (player) playerPosition = player.transform.position;
71
72 ShowIntro();
73 //GlobalVarManager.instance.SetScoreValue(0);
74 }
75
76 void ShowIntro()
77 {
78 isGameStart = false;
79 isGameOver = false;
80 if (!player) return;
81 player.transform.position = playerPosition;
82 //player.pos = 0;
83 player.gameManager = this;
84 player.OnRight();
85 TweenMove(titleLabel, Vector3.up * 700f, posTitleLabel);
86 TweenMove(readyLabel, Vector3.down * 700f, posReadyLabel);
87 TweenMove(tabLabel, tabLabel.localPosition, posTabLabel);
88 StartCoroutine(DelayActoin(0.2f, () =>
89 {
90 TweenMove(startButton, Vector3.down * 700f, posStartButton);
91 scoreLabel.text = "0";
92 }));
93
94 DisplayScore();
95 }
96
97 void LoadOldScore()
98 {
99 oldScore = GlobalVarManager.instance.GetScoreValue();
100 }
101
102 void DisplayScore()
103 {
104 oldScoreLabel.text = oldScore.ToString();
105 }
106
107
108 void SaveNewScore()
109 {
110 LoadOldScore();
111 if (oldScore < score)
112 {
113 GlobalVarManager.instance.SetScoreValue(score);
114 oldScore = score;
115 }
116 }
117
118 public void DoneShakeCam()
119 {
120 cam.transform.position = camPosition;
121 }
122
123 public void TweenMove(Transform tr, Vector3 pos1, Vector3 pos2)
124 {
125 tr.localPosition = pos1;
126 TweenParms parms = new TweenParms().Prop("localPosition", pos2).Ease(EaseType.EaseOutQuad);
127 HOTween.To(tr, 0.4f, parms);
128 }
129
130 public void ShakeCam()
131 {
132 cam.transform.position = camPosition + Vector3.right * 0.2f;
133 TweenParms parms = new TweenParms().Prop("localPosition", camPosition).Ease(EaseType.EaseOutBounce).OnComplete(DoneShakeCam);
134 HOTween.To(cam.transform, 0.4f, parms);
135 }
136
137 public void DoneDamageEffect()
138 {
139 damageSprite.color = new Color(1f, 1f, 1f, 0f);
140 damageSprite.enabled = false;
141 }
142
143 public void DamageEffect()
144 {
145 damageSprite.color = new Color(1f, 1f, 1f, 0f);
146 damageSprite.enabled = true;
147 TweenParms parms = new TweenParms().Prop("color", new Color(1f,1f,1f,0.5f)).Ease(EaseType.Linear).Loops(2,LoopType.Restart).OnComplete(DoneDamageEffect);
148 HOTween.To(damageSprite, 0.05f, parms);
149 }
150
151 public void AddScore()
152 {
153 score++;
154 scoreLabel.text = score.ToString();
155 player.PlayGoodSound();
156 }
157
158 public void StartGame()
159 {
160 if (isGameStart) return;
161 StopCoroutine("SpawnPillar");
162 //pool.DespawnAll();
163 score = 0;
164 startButton.localPosition = Vector3.up * 10000f;
165 isGameStart = true;
166 player.StartPlayer();
167 StartCoroutine("SpawnPillar");
168 TweenMove(titleLabel, posTitleLabel, Vector3.up * 700f);
169 TweenMove(startButton, posStartButton, Vector3.down * 700f);
170 StartCoroutine(DelayActoin(0.2f, () =>
171 {
172 TweenMove(readyLabel, posReadyLabel, Vector3.down * 700f);
173 }));
174 StartCoroutine(DelayActoin(0.5f, () =>
175 {
176 TweenMove(tabLabel, posTabLabel, Vector3.right * 700f);
177 }));
178 }
179
180 public void ReloadGame()
181 {
182 Application.LoadLevel(Application.loadedLevel);
183
184 if (player.transform.position.y > -2f) return;
185 HideGameOver();
186 StartCoroutine(DelayActoin(0.5f, () =>
187 {
188 ShowIntro();
189 }));
190 }
191
192 public void StopGame()
193 {
194 if (!isGameOver)
195 {
196 StopCoroutine("SpawnPillar");
197 SaveNewScore();
198 ShowGameOver();
199 }
200 isGameOver = true;
201 }
202
203
204 void ShowGameOver()
205 {
206 resultScore.text = score.ToString();
207 resultBestScore.text = oldScore.ToString();
208 TweenMove(overLabel, Vector3.up * 700f, posOverLabel);
209 TweenMove(overButton, Vector3.down * 700f, posOverButton);
210 TweenMove(rankBoard, Vector3.right * 700f, posRankBoard);
211 }
212
213 void HideGameOver()
214 {
215 TweenMove(overLabel, posOverLabel, Vector3.up * 700f);
216 TweenMove(overButton, posOverButton, Vector3.down * 700f);
217 TweenMove(rankBoard, posRankBoard, Vector3.right * 700f);
218 //pool.DespawnAll();
219 }
220
221 IEnumerator SpawnPillar()
222 {
223 yield return new WaitForSeconds(spawnTime);
224 /*
225 Transform tr = pool.Spawn(pillarPrefab.transform, Vector3.right * 5f + Vector3.down * Random.Range(1f, 7f), Quaternion.identity);
226 Pillar pillar = tr.GetComponent<Pillar>();
227 pillar.gameManager = this;
228 pillar.ok = false;
229 pillar.pool = pool;
230 */
231 GameObject go = Instantiate(pillarPrefab, Vector3.right * 5f + Vector3.down * Random.Range(1f, 7f), Quaternion.identity) as GameObject;
232 Pillar pillar = go.GetComponent<Pillar>();
233 pillar.gameManager = this;
234 pillar.ok = false;
235 //pillar.collider2D.enabled = true;
236 if (isGameStart && !isGameOver) StartCoroutine("SpawnPillar");
237 }
238
239 void Update () {
240 if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit();
241 if (isGameStart && !isGameOver)
242 if (Input.GetMouseButtonDown(0))
243 {
244 player.OnJump();
245 }
246 //if (Input.GetMouseButtonDown(0)) if (isGameStart && isGameOver) Application.LoadLevel(Application.loadedLevel);
247 }
248
249 public void GoBuntGames()
250 {
251 Application.OpenURL("http://facebok.com/buntgames");
252 }
253
254 public void OpenLeaderBoard()
255 {
256 #if UNITY_IPHONE
257 GlobalVarManager.instance.ShowLeaderboard();
258 #else
259 Application.OpenURL("http://youtube.com/textcube");
260 #endif
261 }
262
263 public IEnumerator DelayActoin(float dtime, System.Action callback)
264 {
265 yield return new WaitForSeconds(dtime);
266 callback();
267 }
268 }
2 using System.Collections;
3 using Holoville.HOTween;
4 using Holoville.HOTween.Plugins;
5 using UnityEngine.UI;
6
7 public class GameManager : MonoBehaviour {
8 public Player player;
9 public GameObject pillarPrefab;
10 public float spawnTime = 4f;
11
12 [HideInInspector]
13 public bool isGameStart = false;
14 [HideInInspector]
15 public bool isGameOver = false;
16 Transform startButton, overButton, readyLabel, overLabel, tabLabel, titleLabel, rankBoard;
17 Vector3 posStartButton, posOverButton, posReadyLabel, posOverLabel, posTabLabel, posTitleLabel, posRankBoard;
18
19 int oldScore = 0;
20 int score = 0;
21
22 Text scoreLabel, resultScore, resultBestScore;
23 Text oldScoreLabel;
24
25 Image damageSprite;
26
27 Camera cam;
28 Vector3 camPosition, playerPosition;
29
30 //public SpawnPool pool;
31
32 void Start () {
33 Application.targetFrameRate = 60;
34 LoadOldScore();
35 cam = Camera.main.GetComponent<Camera>();
36 camPosition = cam.transform.position;
37 resultScore = GameObject.Find("ResultScore").GetComponentInChildren<Text>();
38 resultBestScore = GameObject.Find("ResultBestScore").GetComponentInChildren<Text>();
39 scoreLabel = GameObject.Find("GameScore").GetComponentInChildren<Text>();
40 oldScoreLabel = GameObject.Find("BestScore").GetComponentInChildren<Text>();
41
42 damageSprite = GameObject.Find("DamageSprite").GetComponent<Image>();
43
44 rankBoard = GameObject.Find("RankBoard").transform;
45 titleLabel = GameObject.Find("GameTitle").transform;
46 readyLabel = GameObject.Find("AreYouReady").transform;
47 overLabel = GameObject.Find("GameOver").transform;
48 tabLabel = GameObject.Find("TapToJump").transform;
49 overButton = GameObject.Find("OverButton").transform;
50 startButton = GameObject.Find("StartButton").transform;
51
52 posRankBoard = rankBoard.localPosition;
53 posTitleLabel = titleLabel.localPosition;
54 posStartButton = startButton.localPosition;
55 posOverButton = overButton.localPosition;
56 posReadyLabel = readyLabel.localPosition;
57 posOverLabel = overLabel.localPosition;
58 posTabLabel = tabLabel.localPosition;
59
60 rankBoard.localPosition = Vector3.right * 700f;
61 titleLabel.localPosition = Vector3.up * 700f;
62 readyLabel.localPosition = Vector3.left * 700f;
63 startButton.localPosition = Vector3.down * 700f;
64 overButton.localPosition = Vector3.down * 700f;
65 overLabel.localPosition = Vector3.up * 700f;
66 tabLabel.localPosition = Vector3.right * 700f;
67
68
69
70 if (player) playerPosition = player.transform.position;
71
72 ShowIntro();
73 //GlobalVarManager.instance.SetScoreValue(0);
74 }
75
76 void ShowIntro()
77 {
78 isGameStart = false;
79 isGameOver = false;
80 if (!player) return;
81 player.transform.position = playerPosition;
82 //player.pos = 0;
83 player.gameManager = this;
84 player.OnRight();
85 TweenMove(titleLabel, Vector3.up * 700f, posTitleLabel);
86 TweenMove(readyLabel, Vector3.down * 700f, posReadyLabel);
87 TweenMove(tabLabel, tabLabel.localPosition, posTabLabel);
88 StartCoroutine(DelayActoin(0.2f, () =>
89 {
90 TweenMove(startButton, Vector3.down * 700f, posStartButton);
91 scoreLabel.text = "0";
92 }));
93
94 DisplayScore();
95 }
96
97 void LoadOldScore()
98 {
99 oldScore = GlobalVarManager.instance.GetScoreValue();
100 }
101
102 void DisplayScore()
103 {
104 oldScoreLabel.text = oldScore.ToString();
105 }
106
107
108 void SaveNewScore()
109 {
110 LoadOldScore();
111 if (oldScore < score)
112 {
113 GlobalVarManager.instance.SetScoreValue(score);
114 oldScore = score;
115 }
116 }
117
118 public void DoneShakeCam()
119 {
120 cam.transform.position = camPosition;
121 }
122
123 public void TweenMove(Transform tr, Vector3 pos1, Vector3 pos2)
124 {
125 tr.localPosition = pos1;
126 TweenParms parms = new TweenParms().Prop("localPosition", pos2).Ease(EaseType.EaseOutQuad);
127 HOTween.To(tr, 0.4f, parms);
128 }
129
130 public void ShakeCam()
131 {
132 cam.transform.position = camPosition + Vector3.right * 0.2f;
133 TweenParms parms = new TweenParms().Prop("localPosition", camPosition).Ease(EaseType.EaseOutBounce).OnComplete(DoneShakeCam);
134 HOTween.To(cam.transform, 0.4f, parms);
135 }
136
137 public void DoneDamageEffect()
138 {
139 damageSprite.color = new Color(1f, 1f, 1f, 0f);
140 damageSprite.enabled = false;
141 }
142
143 public void DamageEffect()
144 {
145 damageSprite.color = new Color(1f, 1f, 1f, 0f);
146 damageSprite.enabled = true;
147 TweenParms parms = new TweenParms().Prop("color", new Color(1f,1f,1f,0.5f)).Ease(EaseType.Linear).Loops(2,LoopType.Restart).OnComplete(DoneDamageEffect);
148 HOTween.To(damageSprite, 0.05f, parms);
149 }
150
151 public void AddScore()
152 {
153 score++;
154 scoreLabel.text = score.ToString();
155 player.PlayGoodSound();
156 }
157
158 public void StartGame()
159 {
160 if (isGameStart) return;
161 StopCoroutine("SpawnPillar");
162 //pool.DespawnAll();
163 score = 0;
164 startButton.localPosition = Vector3.up * 10000f;
165 isGameStart = true;
166 player.StartPlayer();
167 StartCoroutine("SpawnPillar");
168 TweenMove(titleLabel, posTitleLabel, Vector3.up * 700f);
169 TweenMove(startButton, posStartButton, Vector3.down * 700f);
170 StartCoroutine(DelayActoin(0.2f, () =>
171 {
172 TweenMove(readyLabel, posReadyLabel, Vector3.down * 700f);
173 }));
174 StartCoroutine(DelayActoin(0.5f, () =>
175 {
176 TweenMove(tabLabel, posTabLabel, Vector3.right * 700f);
177 }));
178 }
179
180 public void ReloadGame()
181 {
182 Application.LoadLevel(Application.loadedLevel);
183
184 if (player.transform.position.y > -2f) return;
185 HideGameOver();
186 StartCoroutine(DelayActoin(0.5f, () =>
187 {
188 ShowIntro();
189 }));
190 }
191
192 public void StopGame()
193 {
194 if (!isGameOver)
195 {
196 StopCoroutine("SpawnPillar");
197 SaveNewScore();
198 ShowGameOver();
199 }
200 isGameOver = true;
201 }
202
203
204 void ShowGameOver()
205 {
206 resultScore.text = score.ToString();
207 resultBestScore.text = oldScore.ToString();
208 TweenMove(overLabel, Vector3.up * 700f, posOverLabel);
209 TweenMove(overButton, Vector3.down * 700f, posOverButton);
210 TweenMove(rankBoard, Vector3.right * 700f, posRankBoard);
211 }
212
213 void HideGameOver()
214 {
215 TweenMove(overLabel, posOverLabel, Vector3.up * 700f);
216 TweenMove(overButton, posOverButton, Vector3.down * 700f);
217 TweenMove(rankBoard, posRankBoard, Vector3.right * 700f);
218 //pool.DespawnAll();
219 }
220
221 IEnumerator SpawnPillar()
222 {
223 yield return new WaitForSeconds(spawnTime);
224 /*
225 Transform tr = pool.Spawn(pillarPrefab.transform, Vector3.right * 5f + Vector3.down * Random.Range(1f, 7f), Quaternion.identity);
226 Pillar pillar = tr.GetComponent<Pillar>();
227 pillar.gameManager = this;
228 pillar.ok = false;
229 pillar.pool = pool;
230 */
231 GameObject go = Instantiate(pillarPrefab, Vector3.right * 5f + Vector3.down * Random.Range(1f, 7f), Quaternion.identity) as GameObject;
232 Pillar pillar = go.GetComponent<Pillar>();
233 pillar.gameManager = this;
234 pillar.ok = false;
235 //pillar.collider2D.enabled = true;
236 if (isGameStart && !isGameOver) StartCoroutine("SpawnPillar");
237 }
238
239 void Update () {
240 if (Input.GetKeyDown(KeyCode.Escape)) Application.Quit();
241 if (isGameStart && !isGameOver)
242 if (Input.GetMouseButtonDown(0))
243 {
244 player.OnJump();
245 }
246 //if (Input.GetMouseButtonDown(0)) if (isGameStart && isGameOver) Application.LoadLevel(Application.loadedLevel);
247 }
248
249 public void GoBuntGames()
250 {
251 Application.OpenURL("http://facebok.com/buntgames");
252 }
253
254 public void OpenLeaderBoard()
255 {
256 #if UNITY_IPHONE
257 GlobalVarManager.instance.ShowLeaderboard();
258 #else
259 Application.OpenURL("http://youtube.com/textcube");
260 #endif
261 }
262
263 public IEnumerator DelayActoin(float dtime, System.Action callback)
264 {
265 yield return new WaitForSeconds(dtime);
266 callback();
267 }
268 }
public SpawnPool pool;
GlobalVarManager.instance.SetScoreValue(0);
player.pos = 0;
pool.DespawnAll();
pool.DespawnAll();
pillar.collider2D.enabled = true;
if (Input.GetMouseButtonDown(0)) if (isGameStart && isGameOver) Application.LoadLevel(Application.loadedLevel);
Application.OpenURL("http:facebok.combuntgames");
Application.OpenURL("http:youtube.comtextcube");